home *** CD-ROM | disk | FTP | other *** search
/ Amoszine 6 / Amoszine 6 (Disk 2 of 2).adf / extra_source.lha / OTHER_SOURCE / _GET_ARGS.Amos / _GET_ARGS.amosSourceCode
Encoding:
AMOS Source Code  |  1992-02-26  |  4.1 KB  |  133 lines

  1. ' This is a routine by Michael D. Cox (aj639@Cleveland.freenet.edu)
  2. ' I was looking through the RKMs and saw an assembler routine to determine 
  3. ' all the command line arguments for a command.  So, I was bored and 
  4. ' decided to convert it to AMOS, which was real simple.
  5. '
  6. ' These lines just setup an example. 
  7. ' _MAXARGC is a variable that has the maximum number of arguments your program   
  8. '          will accept.  
  9. '
  10. ' ARGV$(_MAXARGC) is an array that will hold the FINAL arguments.
  11. ' CMDLINE$ will actually become CMDLINE$ = Command Line$ when you have 
  12. '          compiled your program and are ready to distribute it. 
  13. ' _TOTAL_ARGS will be assigned the actual number of arguments
  14. '
  15. ' Unfold the procedure and it will explain what happens
  16. '
  17. ' You may modify this to handle whatever other type of arguments you need
  18. ' but please send me a copy just so I can see what changes were made.
  19. ' Thanks!! 
  20. ' Michael Cox, October 7, 1993,  19:10 EDT 
  21. ' (703)742-2991 (voice), (703)742-2543 (fax) 
  22. ' 11128 Rock Garden Drive, Fairfax VA 22030-4935 USA 
  23. '
  24. _MAXARGC=6
  25. Dim ARGV$(_MAXARGC)
  26. Shared ARGV$()
  27. CMDLINE$="first second "+Chr$(34)+"third arg"+Chr$(34)+" "+Chr$(34)+"*Njim"+Chr$(34)+" *E4"
  28. Print CMDLINE$
  29. _GET_ARGS[CMDLINE$,_MAXARGC]
  30. _TOTAL_ARGS=Param
  31. For X=0 To _TOTAL_ARGS-1
  32.    Print ARGV$(X)
  33. Next 
  34. Procedure _GET_ARGS[CMDLN$,ARGC]
  35.    ' Here we just set the array to empty strings
  36.    For X=0 To ARGC-1
  37.       ARGV$(X)=""
  38.    Next X
  39.    ' We now assign our needed variables 
  40.    ' CMDLN_LEN = Length of the CMDLN$ so we know when we have reached the end 
  41.    CMDLN_LEN=Len(CMDLN$)
  42.    ' CURR_ARG = Current command line argument we are processing 
  43.    CURR_ARG=0
  44.    ' CURR_POS = Position within CMDLN$
  45.    CURR_POS=1
  46.    '
  47.    ' We begin to find the first character in the argument 
  48.    _START_ARG:
  49.    If CURR_POS>CMDLN_LEN : Rem Make sure we have not gone past EOL
  50.       Goto ARG_EXIT
  51.    End If 
  52.    ' Compare current chr to a space or a tab (chr$(9)) which seperate args  
  53.    If(Mid$(CMDLN$,CURR_POS,1)=" ") or(Mid$(CMDLN$,CURR_POS,1)=Chr$(9))
  54.       Inc CURR_POS
  55.       Goto _START_ARG
  56.    End If 
  57.    ' Just a check to make sure we have not found an extra arg 
  58.    If(CURR_ARG+1)>ARGC
  59.       Goto ARG_EXIT
  60.    End If 
  61.    ' See if the chracter is a double quote (")
  62.    If Mid$(CMDLN$,CURR_POS,1)=Chr$(34)
  63.       Inc CURR_POS
  64.       Goto _QUOTE
  65.    End If 
  66.    ' Well, if we get to here we have found the first character  
  67.    ARGV$(CURR_ARG)=Mid$(CMDLN$,CURR_POS,1)
  68.    Inc CURR_POS
  69.    '
  70.    ' Begin looking for the next character 
  71.    _NEXT_CHR:
  72.    If CURR_POS>CMDLN_LEN
  73.       Goto ARG_EXIT
  74.    End If 
  75.    If(Mid$(CMDLN$,CURR_POS,1)=" ") or(Mid$(CMDLN$,CURR_POS,1)=Chr$(9))
  76.       Goto _END_ARG
  77.    End If 
  78.    ' If it is not a space or tab or end of the line, must be the next character 
  79.    ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Mid$(CMDLN$,CURR_POS,1)
  80.    Inc CURR_POS
  81.    ' So, find the next one and keep going . . . 
  82.    Goto _NEXT_CHR
  83.    '
  84.    ' We found all the characters in an argument 
  85.    _END_ARG:
  86.    Inc CURR_ARG
  87.    Inc CURR_POS
  88.    Goto _START_ARG
  89.    '
  90.    ' Our routine to handle quotes which are used to put around arguments that 
  91.    ' may have spaces or other special characters in them. 
  92.    _QUOTE:
  93.    If CURR_POS>CMDLN_LEN
  94.       Goto ARG_EXIT
  95.    End If 
  96.    If Mid$(CMDLN$,CURR_POS,1)=Chr$(34)
  97.       Goto _END_ARG
  98.    End If 
  99.    ' A splat (*) is the BCPL equivalent to the ESC command for doing ANSI codes 
  100.    If Mid$(CMDLN$,CURR_POS,1)<>"*"
  101.       Goto _ADD_QUOTE_CHR
  102.    End If 
  103.    Inc CURR_POS
  104.    ' A *N is a newline or chr$(10)
  105.    If Upper$(Mid$(CMDLN$,CURR_POS,1))<>"N"
  106.       Goto _CHECK_ESC
  107.    End If 
  108.    ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Chr$(10)
  109.    Inc CURR_POS
  110.    Goto _QUOTE
  111.    '
  112.    ' A *E is an actual ESC or chr$(27)
  113.    _CHECK_ESC:
  114.    If Upper$(Mid$(CMDLN$,CURR_POS,1))<>"E"
  115.       Goto _ADD_QUOTE_CHR
  116.    End If 
  117.    ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Chr$(27)
  118.    Inc CURR_POS
  119.    Goto _QUOTE
  120.    '
  121.    ' We made it to here, meaning there were no special escape codes found 
  122.    ' so add it to the current argument string 
  123.    _ADD_QUOTE_CHR:
  124.    ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Mid$(CMDLN$,CURR_POS,1)
  125.    Inc CURR_POS
  126.    Goto _QUOTE
  127.    '
  128.    ' Goto here when we have found all arguments 
  129.    ARG_EXIT:
  130.    CMDLN$=""
  131.    CMDLN_LEN=0
  132.    CURR_POS=0
  133. End Proc[CURR_ARG+1]